package cn.melon.helper;

import org.apache.log4j.Logger;

/**
 * 
 * 该类负责日志记录, 使用时 
 * <code>
 *      private String TAG = LoggerHelper.makeLogTag(Some.class):
 *      private Logger logger = LoggerHelper.clientLogger();
 * </code>
 * 
 * @author corvo
 * @date Aug 26, 2016
 */
public class LoggerHelper {

    private static final String LOG_PREFIX = "MELON_";
    private static final int LOG_PREFIX_LENGTH = LOG_PREFIX.length();
    private static final int MAX_LOG_TAG_LENGTH = 23;
    private static Logger logger = null;

    public static String makeLogTag(String str) {
        if (str.length() > MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH) {
            return LOG_PREFIX + str.substring(0, MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH);
        }
        return "[" + LOG_PREFIX + str + "]  ";
    }
    
    public static String makeLogTag(@SuppressWarnings("rawtypes") Class cls) {
        return makeLogTag(cls.getSimpleName());
    }
    
    public static Logger clientLogger() {
        if (logger == null) {
            logger = Logger.getLogger("MELON_CLIENT");
        }
        return logger;
    }
    
    public static Logger serverLogger() {
        if (logger == null) {
            logger = Logger.getLogger("MELON_SERVER");
        }
        return logger;
    }
    
}
